package net.wanho.productManage.service.impl;

import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.service.impl.BaseServiceImpl;
import net.wanho.productManage.dao.OrdersDAO;
import net.wanho.productManage.dao.OrdersDetailDAO;
import net.wanho.productManage.dao.ProductDAO;
import net.wanho.productManage.dao.ProductInventoryDAO;
import net.wanho.productManage.po.Orders;
import net.wanho.productManage.po.OrdersDetail;
import net.wanho.productManage.po.Product;
import net.wanho.productManage.po.ProductInventory;
import net.wanho.productManage.service.OrdersService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.DoubleStream;

public class OrdersServiceImpl extends BaseServiceImpl<OrdersDAO, Orders> implements OrdersService {
    private OrdersDetailDAO ordersDetailDAO  ;
    private ProductDAO productDAO ;
    private ProductInventoryDAO productInventoryDAO ;

    @Override
    public boolean submitOrder(Orders orders) throws ServiceException {
        //1. 根据订单序号进行查询
        String orderSeq = orders.getOrderSeq();
        Orders dbOrders = super.baseDAO.selectByOrderSeq(orderSeq);
        //把 数据库中 orders 合并 用户请求参数的 orders 进行合并
        orders.setOrderId(dbOrders.getOrderId());
        orders.setOrderType(dbOrders.getOrderType());
        orders.setStatus("1");
        //设置订单详情 关联属性
        orders.getOrdersDetailList().forEach(ordersDetail -> {
            ordersDetail.setOrders(orders);
            BigDecimal total = ordersDetail.getRealPrices().multiply(new BigDecimal(ordersDetail.getBuyNums())) ;
            ordersDetail.setRealTotalprice(total);
            //设置总金额
        });
        double sum = orders.getOrdersDetailList().stream().mapToDouble(od -> od.getRealTotalprice().doubleValue()).sum();
        orders.setTotalMoney(new BigDecimal(sum));
        //2. 修改订单
        super.baseDAO.update(orders) ;
        //3. 向订单详情表插入N条数据
        ordersDetailDAO.insertBatch(orders.getOrdersDetailList()) ;
        return true ;
    }

    @Override
    public boolean auditOrders(String orderSeq) throws ServiceException {
        //根据orderSeq 进行查询
        Orders orders = super.baseDAO.selectByOrderSeq(orderSeq);
        //根据订单编号 查询订单详情
        Map<String,String> condMap = new HashMap<>(1) ;
        condMap.put("ORDER_ID",orders.getOrderId()+"") ;
        //查询该订单关联的详情
        List<OrdersDetail> ordersDetailList = ordersDetailDAO.selectByCond(condMap);
        //根据商品id查询商品信息
        for (OrdersDetail ordersDetail : ordersDetailList) {
            //比较商品价格 与  订单详情价格
            Product dbProduct = productDAO.findById(ordersDetail.getProduct().getProductId() + "");
            //判断商品状态 [下架状态的商品] 或者价格不一致
            if(dbProduct.getStatus().equals("0") || !dbProduct.getCostPrice().equals(ordersDetail.getRealPrices())){
                //修改订单状态为 8 审核失败
                orders.setStatus("8");
                super.baseDAO.update(orders) ;
                throw new ServiceException("商品状态 [下架状态的商品] 或者价格不一致!!!") ;
            }
        }
        //修改订单状态
        orders.setStatus("2");
        super.baseDAO.update(orders) ;
        return true;
    }

    @Override
    public boolean outOfStock(String orderSeq) throws ServiceException {
        //根据orderSeq 进行查询
        Orders orders = super.baseDAO.selectByOrderSeq(orderSeq);
        //根据订单编号 查询订单详情
        Map<String,String> condMap = new HashMap<>(1) ;
        condMap.put("ORDER_ID",orders.getOrderId()+"") ;
        //查询该订单关联的详情
        List<OrdersDetail> ordersDetailList = ordersDetailDAO.selectByCond(condMap);
        //需要修改的库存数量
        List<ProductInventory> productInventoryList = new ArrayList<>(ordersDetailList.size()) ;
        //根据商品id查询商品信息
        for (OrdersDetail ordersDetail : ordersDetailList) {
            //查询仓库进行 修改
            ProductInventory productInventory = productInventoryDAO.selectByProductIdAndStoreId(ordersDetail.getProduct().getProductId(), ordersDetail.getStore().getStoreId());
            //当前仓库 没有商品 或 仓库数据不够
            if (productInventory==null || productInventory.getInventory().compareTo(new BigDecimal(ordersDetail.getBuyNums()))<1){
                //修改订单状态为 8 审核失败
                orders.setStatus("9");
                super.baseDAO.update(orders) ;
               throw new ServiceException("当前仓库 没有商品 或 仓库数据不足,请联系采购员！！！") ;
            }
            //修改库存数量
            productInventory.setInventory(productInventory.getInventory().subtract(new BigDecimal(ordersDetail.getBuyNums())));
            //添加到要修改的集合中
            productInventoryList.add(productInventory) ;
        }
        //修改订单状态
        orders.setStatus("3");
        super.baseDAO.update(orders) ;
        //批量修改 库存
        productInventoryDAO.updateBatch(productInventoryList) ;
        return true;
    }
}
